<?php
class system extends table
{
	public function make($action,$wid,$classid=null,$id=null,$preview='preview',$print=0, $page=1)
	{
		if ($print)
		{
			set_time_limit(0);
			header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
			header('Cache-Control: no-cache, must-revalidate');
			header('Pragma: no-cache');
			header("Content-Encoding: gzip off ");
			ob_implicit_flush(true);
			ob_end_flush();
			echo str_pad(" ", 2560);
		}
		$this->wid		= $wid;
		$this->classid	= $classid;
		$this->id		= $id;
		$this->action	= $action;
		$this->columns	= base::init('column')->fetchAll(array('wid'=>$this->wid));
		$this->website	= base::init('website')->fetchRow(array('wid'=>$this->wid));
		$this->mode		= sort_combine(base::init('mode')->fetchAll(), 'mid', true);
		$this->attr		= base::init('website')->getConfig($this->website['domain']);
		$this->infotype	= base::init('infotype')->fetchAll();
		
		$core			= new core;
		$this->template	= $core->template();
		
		$this->template->template_dir	= ROOT . $this->attr['tempdir'];
	    $this->template->compile_dir	= ROOT . $this->attr['tempdir'] . "/template_c";
	    
	    $this->template->assign('wid', $this->wid);
	    $this->template->assign('classid', $this->classid);
	    $this->template->assign('id', $this->id);
	    $this->template->assign('columns', $this->columns);
	    $this->template->assign('website', $this->website);
	    $this->template->assign('mode', $this->mode);
	    $this->template->assign('attr', $this->attr);
	    $this->template->assign('infotype', $this->infotype);
		
		if (in_array('index',$this->action))
		{
			//模板文件名
			$temp       = isset($this->attr['indextemp']) ? $this->attr['indextemp'] : 'index.htm';
			//模板文件绝对路径
			$temp_file  = ROOT . $this->attr['tempdir'] . $temp;
			//生成目录
			$path		= ROOT . $this->attr['arcdir'];
			//生成文件绝对路径
			$file_path	= $path . '/index' . $this->attr['indextype'];

			$html		= $this->template->fetch($temp_file);
			$this->html($html,$file_path,$preview,$print);
		}
		if (in_array('column',$this->action))
		{
			if ('wid' == $this->classid)
			{
				$this->classid	= array_get_unique($this->columns, 'classid');
			}
			else //if (strstr(',',$this->classid))
			{

			}

			foreach ($this->classid as $val)
			{
				$column		= $this->columns[$val];
				$this->template->assign('column', $column);
				//模板文件名
		    	$temp       = $column['listtemp'];
		    	//生成目录
		    	$path		= ROOT . $this->attr['arcdir']."/{$column['classpath']}";
		    	//生成文件绝对路径
				$file_path	= ROOT . $this->attr['arcdir']."/{$column['classpath']}/{$column['defaultname']}";
				$html		= trim($this->template->fetch($temp));
				
				if ('preview' == $preview && $page == 1)
				{
					$this->html($html,$file_path,$preview,null);
				}
				else if ('preview' == $preview && $page !== 1)
				{
					$newsurl	= $column['classpath'];
					$this->template->_tpl_vars['thispage']	= $page;
					eval("\$file_path=\"{$column['classextension']}\";");

					$html		= trim($this->template->fetch($temp));
					#$title		= $column['classname'].'-'.$i;
					
					$realpath	= ROOT . $this->attr['arcdir'] . $file_path;
					$url		= !empty($column['domain']) ? $column['domain'] : $this->attr['url'].$file_path;
					
					$this->html($html,$realpath,$preview,null);
				}
				else 
				{
					$print	= $print ? "{$column['classname']}已生成" : 0;
					$this->html($html,$file_path,$preview,$print);
					if (isset($this->template->_tpl_vars['pager']))
					{
						for ($i=$this->template->_tpl_vars['thispage']+1;$i<=$this->template->_tpl_vars['pager']['maxpage'];$i++)
						{
							$newsurl	= $column['classpath'];
							$page		= $this->template->_tpl_vars['thispage']	= $i;
							eval("\$file_path=\"{$column['classextension']}\";");
		
							$html		= trim($this->template->fetch($temp));
							$title		= $column['classname'].'-'.$i;
							
							$realpath	= ROOT . $this->attr['arcdir'] . $file_path;
							$url		= !empty($column['domain']) ? $column['domain'] : $this->attr['url'].$file_path;
							$print	= $print ? "<a href='$file_path' target='_blank'>{$title}</a>已生成" : 0;
							$this->html($html,$realpath,$preview,$print);
							usleep(500);
						}
					}
					unset(
						$this->template->_tpl_vars['thispage'],
						$this->template->_tpl_vars['pager'],
						$this->template->_tpl_vars['list']
					);
				}
				#usleep(1000);
			}
		}
		if (in_array('page',$this->action))
		{
			if ('wid' == $this->id)
			{
				$this->classid	= array_get_unique($this->columns, 'classid');
				$getCountQuery	= array('classid'=>$this->classid);
			}
			else if ('classid' == $this->id)
			{
				$getCountQuery	= array('classid'=>$this->classid);
			}
			else 
			{
				$getCountQuery	= array('id'=>$this->id);
			}
			
			$modid	= base::init('column')->fetchModids($wid);
        	$count	= 0 ;
        	
        	foreach ($this->classid as $val)
        	{
        		$column		= $this->columns[$val];
        		$tbname	= $this->mode[$column['modid']]['tbname'];
        		$count	= base::init($tbname)->getCount($getCountQuery);

        		if ($count)
        		{
        			for ($i=0;$i<$count;$i++)
        			{
        				$page		= array_shift(base::init($tbname)->fetchAll($getCountQuery,'',$i+1,$i));
        				$classid	= $page['classid'];
						$column		= $this->columns[$classid];
						$temp       = $column['contenttemp'];
						$file_path	= "{$column['classpath']}";
						$newsurl	= $file_path;
						$tag		= dict($page['title']);
						
					    $aid	 	= isset($page['aid']) ? $page['aid'] : $page['id'];
					    
					    
					    
					    eval("\$file_path = \"{$column['contentextension']}\";");
					    $realpath	= ROOT . $this->attr['arcdir'].$file_path;
					    
					    $link		= $this->attr['url'].$file_path;

					    $this->template->assign('column', $column);
						$this->template->assign('page', $page);
						$this->template->assign('tag',$tag);
						$this->template->assign('link',$link);
						
						$html = trim($this->template->fetch($temp));
					    $print	= $print ? "<a href='$file_path' target='_blank'>{$page['title']}</a>已生成" : 0;
					    $this->html($html,$realpath,$preview,$print);
        			}
        		}
        		#usleep(1000);
        	}
		}
	}
	
	public function html($html,$path,$type,$print)
	{
		if ('make'  == $type)
		{
			is_dir($path) || mdir(dirname($path));
		
			$fp	= fopen($path, 'w');
			fwrite($fp, $html);
		}
		else if ('preview' == $type)
		{
			print_r($html);
		}
		if ($print)
		{
			echo $print."<br />\n";
			@ob_flush();flush();
		}
	}
}
